/**
 * Created by sunkejun on 2015/10/15.
 */

$(document).ready(function(){
    var table = new ComDataTable("/admin/role/page",$('#dataTable'),"admin_roles");
    var renders = {
        id:admin_roles_list.editRender,
        photo_url:admin_roles_list.photo_render,
    };
    table.setRenders(renders);
    table.init();
    admin_roles_list.$table=table;
    //<span class="label label-primary">Enable</span>
})
var admin_roles_list  = (function ($) {
    this.editRender = function( data, type, full, meta ) {
        //return '<div class=" btn-group">'
        //    +'<a class="btn btn-xs btn-white" onclick="admin_roles_list.editTarget('+data+')" ><i class="fa fa-wrench"></i> </a>'
        //    +' <a class="btn btn-xs btn-white" onclick="admin_roles_list.delete('+data+')"><i class="fa fa-minus"></i> </a>'
        //    +'</div>';

        return '<button class="btn btn-sm btn-success" tooltip="角色分配" onclick="admin_roles_list.choseResouce('+data+')"  >  <span class="fa fa-users"></span></button>'+
            '&nbsp;&nbsp<button class="btn btn-sm btn-warning"  tooltip="修改"  onclick="admin_roles_list.editTarget('+data+')" >  <span class="fa fa-edit"></span>  </button>'+
            '&nbsp;&nbsp<button class="btn btn-sm btn-danger"  tooltip="删除"   onclick="admin_roles_list.delete('+data+')">  <span class="fa fa-trash"></span></button>'
    }
    this.photo_render = function( data, type, full, meta ) {
        return '<img alt="image" id="navigation_headimg" class="img-circle" src="'+data+'">';
    }



    this.delete = function(id){
        swal({
            title: language.getNorlanguage().delete_title,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: language.getNorlanguage().delete_sub,
            cancelButtonText: language.getNorlanguage().delete_cancle,
            closeOnConfirm: true,
            html: false
        }, function(){
            ComdataUtil.ajaxpostnormal('/admin/role/'+id,{},function(data){
                this.$table.rdraw();
            },"DELETE");
        });

        //ComdataUtil.ajaxpostnormal('/admin/message/'+id,{},function(data){
        //
        //},"DELETE");
    }

    this.initTree = function(data){
        var setting = {
            check: {
                enable: true,
                chkboxType :  { "Y" : "ps", "N" : "ps" }
            },
            callback: {
                onClick: this.treeCategorySelect
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "parent_id"
                }
            }
        };
        var zNodes =data;
        this.$tree = $.fn.zTree.init($("#category_tree"), setting, zNodes);
        //var nodes = this.$tree.getNodesByParam("id", $("#parent_id").val(), null);
        //
        //if(nodes.length >0){
        //    this.$tree.selectNode(nodes[0], true, true);
        //}
    }
    this.treeCategorySelect = function(event, treeId, treeNode){
        //alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);

        $("#category_modal_current").html(treeNode.name);

    }


    this.addNew = function(){
        openModel(0);
    }
    this.getItem = function(item){
        //openModel();
        ComdataUtil.loadContainer("actionmodifyform",item);
    }
    this.editTarget = function(id){
        openModel(1);
        ComdataUtil.ajaxpostnormal('/admin/role/'+id,{},this.getItem,"GET");

    }

    this.openModel = function(type) {

        $("#role_modify_modal").modal('show');
    }
    this.closeModel = function() {
        $("#role_modify_modal").modal('hide');
    }
    //角色分配资源
     this.choseResouce = function(id) {
        ComdataUtil.ajaxpostnormal('/admin/role/create',{id:id},function(data){
            $("#role_resource_modal").modal('show');
            $("#role_id").val(id);
            initTree(data);
        },'GET');

    }
    this.submitResource = function(){
        var role_id =  $("#role_id").val();
        var nodes =  this.$tree.getCheckedNodes(true);
        var noids = [];
        for(i=0;i<nodes.length;i++){
            noids.push(nodes[i].id);
        }
        ComdataUtil.ajaxpost('/admin/role/saveRoleResource',{role_id:role_id,noids:noids.join()},function(data){
            $("#role_resource_modal").modal('hide');
        });
    }

    this.submit = function(){
        $('#role_modify_message').html("");
        var data = ComdataUtil.collectData("actionmodifyform");
        ComdataUtil.ajaxpost('/admin/role',data,this.finishAjaxLoadBack);
    }
    this.finishAjaxLoadBack = function(response){
        if(response.code == 500){
            console.log(response.res_data);
            $('#role_modify_message').attr("class","text-danger text-center");
            $('#role_modify_message').html(response.res_data);
        }else{
            ComdataUtil.clear("actionmodifyform");
            closeModel();
            this.$table.rdraw();
        }

    }
    this.resetCategory = function(){
        ComdataUtil.clear("actionmodifyform");
    }

    return this;
})(jQuery);

